package top.songxs.yygh.service.order.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.songxs.yygh.common.result.R;
import top.songxs.yygh.service.order.service.PaymentService;
import top.songxs.yygh.service.order.service.WeixinService;

import java.util.Map;

@RestController
@RequestMapping("api/order/weixin")
public class WeiXinController {
    @Autowired
    WeixinService weixinService;

    /**
     * 订单详情页，点击支付按钮时被调用,
     * 返回支付链接,前端通过插件将支付链接装换为二维码
     */
    @GetMapping("/createNative/{orderId}")
    public R createNative(@PathVariable("orderId") Long orderId) {

        Map<String,Object> map = weixinService.createNative(orderId);

        return R.ok().data(map);
    }


    @Autowired
    private PaymentService paymentService;

    @ApiOperation(value = "查询支付状态")
    @GetMapping("/queryPayStatus/{orderId}")
    public R queryPayStatus(@PathVariable("orderId") Long orderId) {
        Map<String,String> map = weixinService.queryPayStatus(orderId);

        if (map == null) {//出错
            return R.error().message("支付出错");
        }
        if ("SUCCESS".equals(map.get("trade_state"))) {//如果成功
            //更改订单状态，处理支付结果
            paymentService.paySuccess(map);
            return R.ok().message("支付成功");
        }
        return R.ok().message("支付中");

    }
}
